<?php
class Default_Model_Cat extends Zend_Db_Table
{
	protected $_name = 'article';
	protected $_primary = 'id';
	
	public function __construct($arrParam)
	{
		$this->_arrParam = $arrParam;
	}
	
	public function getItems()
	{
		$cache = new Zendvn_Cache();
		$paginator  = $this->_arrParam['paginator'];
		if ( ! $result = $cache->load('article_cid_' . $this->_arrParam['cid'] .'_page_'. $paginator['currentPage'])) {
			$db = Zend_Registry::get('connectDb');
			$select = $db->select()
				->from('article', array('id', 'title'))
				->order('title', 'ASC')
				->where('status = ?', 1, INTEGER)
				->where('cat IN (SELECT `id` FROM `catalogue` WHERE `parent` = ?)', $this->_arrParam['cid'], INTEGER)
				->limitPage($paginator['currentPage'], $paginator['itemCountPerPage']);
			$result = $db->fetchAll($select);
			$cache->save($result, 'article_cid_' . $this->_arrParam['cid'] .'_page_'. $paginator['currentPage']);
		}
		return $result;
	}
	
	public function getTotal()
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
			->from('article', array('id'))
			->where('status = ?', 1, INTEGER)
			->where('cat IN (SELECT `id` FROM `catalogue` WHERE `parent` = ?)', $this->_arrParam['cid'], INTEGER);
		$result = $db->fetchAll($select);
		return sizeof($result);
	}
}